using System;
using System.Collections;
using System.Data;
using CE.Database;
using CE.Security;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.User
{
    public class ProfileDB
    {
        public ProfileDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(Profile profile)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocUserProfileInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProfileID", profile.ProfileId);
            _sPara.Add("@Salutation", profile.Salutation);
            _sPara.Add("@FirstName", profile.FirstName == string.Empty ? string.Empty : profile.FirstName);
            _sPara.Add("@SurName", profile.SurName == string.Empty ? string.Empty : profile.SurName);
            _sPara.Add("@DOB", profile.DOB);
            _sPara.Add("@Gender", profile.Gender);
            _sPara.Add("@MaritalStatus", profile.MaritalStatus);
            _sPara.Add("@OccupationType", profile.OccupationType);
            _sPara.Add("@Occupation", profile.Occupation);
            _sPara.Add("@IDNOType", profile.IDType == string.Empty ? string.Empty : profile.IDType);
            _sPara.Add("@IDNO", profile.IDNo == string.Empty ? string.Empty : profile.IDNo);
            _sPara.Add("@HighEducation", profile.HighEducation);
            _sPara.Add("@Nationality", profile.Nationality == string.Empty ? string.Empty : profile.Nationality);
            _sPara.Add("@MontlyIncome", profile.MontlyIncome);
            _sPara.Add("@JobNature", profile.JobNature);
            _sPara.Add("@YearDriveSg", profile.YearDriveSg);
            _sPara.Add("@OtherIncome", profile.OtherIncome);
            _sPara.Add("@RefCEUserId", profile.RefCEUserId);
            _sPara.Add("@Status", profile.Status);
            _sPara.Add("@Info1", profile.Info1);
            _sPara.Add("@Info2", profile.Info2);
            _sPara.Add("@Info3", profile.Info3);
            _sPara.Add("@Info4", profile.Info4);
            _sPara.Add("@Info5", profile.Info5);
            _sPara.Add("@Info6", profile.Info6);
            _sPara.Add("@Info7", profile.Info7);
            _sPara.Add("@Info8", profile.Info8);
            _sPara.Add("@Info9", profile.Info9);
            _sPara.Add("@Info10", profile.Info10);
            _sPara.Add("@Info11", profile.Info11);
            _sPara.Add("@Info12", profile.Info12);
            _sPara.Add("@Info13", profile.Info13);
            _sPara.Add("@Info14", profile.Info14);
            _sPara.Add("@Info15", profile.Info15);
            _sPara.Add("@Info16", profile.Info16);
            _sPara.Add("@Info17", profile.Info17);
            _sPara.Add("@Info18", profile.Info18);
            _sPara.Add("@Info19", profile.Info19);
            _sPara.Add("@Info20", profile.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public Profile GetProfile(Int64 RefCEUserId)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocUserProfileSelectSingleItem";
            Profile _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@RefCEUserId", RefCEUserId);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new Profile();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }
        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }

        private Profile FillDataRecord(DataTable myDataTable)
        {
            Profile myProfile = new Profile();
            myProfile.ProfileId = Convert.ToInt64(myDataTable.Rows[0]["ProfileID"].ToString());
            myProfile.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myProfile.Salutation = myDataTable.Rows[0]["Salutation"].ToString();

            string _firstname = myDataTable.Rows[0]["FirstName"].ToString();
            if (_firstname != string.Empty)
            {
                myProfile.FirstName = _firstname;
            }

            string _surname = myDataTable.Rows[0]["SurName"].ToString();
            if (_surname != string.Empty)
            {
                myProfile.SurName = _surname;
            }

            myProfile.DOB = Convert.ToDateTime(myDataTable.Rows[0]["DOB"].ToString());
            myProfile.Gender = Convert.ToChar(myDataTable.Rows[0]["Gender"].ToString());
            myProfile.MaritalStatus = Convert.ToInt32(myDataTable.Rows[0]["MaritalStatus"].ToString());
            myProfile.OccupationType = Convert.ToInt32(myDataTable.Rows[0]["OccupationType"].ToString());
            myProfile.Occupation = Convert.ToInt32(myDataTable.Rows[0]["Occupation"].ToString());

            string _idtype = myDataTable.Rows[0]["IDNoType"].ToString();
            if (_idtype != string.Empty)
            {
                myProfile.IDType = _idtype;
            }

            string _idno = myDataTable.Rows[0]["IDNo"].ToString();
            if (_idno != string.Empty)
            {
                myProfile.IDNo = _idno;
            }
            myProfile.HighEducation = myDataTable.Rows[0]["HighEducation"].ToString();

            string _nationality = myDataTable.Rows[0]["Nationality"].ToString();
            if (_nationality != string.Empty)
            {
                myProfile.Nationality = _nationality;
            }

            myProfile.MontlyIncome = Convert.ToSingle(myDataTable.Rows[0]["MontlyIncome"].ToString());
            myProfile.OtherIncome = Convert.ToSingle(myDataTable.Rows[0]["OtherIncome"].ToString());
            myProfile.JobNature = myDataTable.Rows[0]["JobNature"].ToString();
            myProfile.YearDriveSg = Convert.ToInt16(myDataTable.Rows[0]["YearDriveSg"].ToString());
            myProfile.Status = myDataTable.Rows[0]["Status"].ToString();
            return myProfile;
        }
    }
}